
import { ElMessage } from "element-plus";
import { newSessionid, getCatesian3FromPX ,createRemindertip,destroyTip} from "../../plotCommon";
const Cesium = window.Cesium;
import CreateRandom from './Createrandom'
export default class Random {
    handler:any;
    _viewer:any
    _option:any
    _entities:Array<any>
    _pickerEntity:any
constructor(viewer:any){
    this._viewer=viewer
    this._entities=[]
}
get options() {
    return this._option;
  }
set options(val){
    val=Object.assign({},val)
  this._option=val
}
get entitis(){
  return this._entities
}
set entitis(val){
  this._entities=val
}
create(){
    let viewer =this._viewer
    if (this.handler) this.handler.destroy();
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    const resultList:any = [];
    const callback = (event:any) => {
      console.log(event, "event");
      this._entities.push(event)
    };
    CreateRandom(viewer, this.handler, resultList, this._option, callback);
}
stopCreate(){

}
edit(){

}
stopEdit(){
    
}
removeByClick(){
    let viewer = this._viewer;
    const calback = (e:any) => {
        console.log(e);
        
      if(e.GeoType == 'FreePolygon')viewer.entities.remove(e);
      else{ElMessage.error('当前点击的不是任意多边形')}
      this.handler.destroy()
    };
    this.handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas);
    this.getPicking(this.handler, viewer, calback, false);
}
getPicking(handler:any, viewer:any, callback:any, openTripParemeter:any) {
    // 创建一个ScreenSpaceEventHandler对象
    let _this = this;
    handler.setInputAction(function (click:any) {
      console.log("getPicking正在拾取坐标");

      var position = click.position;
      var pickedObject = viewer.scene.pick(position);
      // 检查pickedObject是否为实体
      if (
        Cesium.defined(pickedObject) &&
        Cesium.defined(pickedObject.id) &&
        pickedObject.id instanceof Cesium.Entity
      ) {
        // 获取点击的实体+
        var entity = pickedObject.id;
        // 输出实体信息
        const toolTip = pickedObject.id;
        _this._pickerEntity = entity;
        if (callback && typeof callback == "function") callback(entity);
        if (openTripParemeter && typeof openTripParemeter == "string")
          createRemindertip(entity[openTripParemeter], click.position, true);
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
}